#include "message_view.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QParallelAnimationGroup>
#include <QSvgWidget>
#include <QPushButton>

MessageView::MessageView(QWidget *parent)
    : QWidget{parent}, viewList{}, parent{parent}
{
}

void MessageView::send(const QString&str, MessgeType color, const unsigned int msec)
{
    qDebug() << str;
    std::pair msgtype = std::move(messageColorMap[color]);
    auto *    item    = new MessageItem(msgtype.first, str, msgtype.second, msec, this->parent);
    item->setPosition(viewList.size() * (messageItemHeight + spacing) + spacing);
    item->show();
    // connect(&item->timer, &QTimer::timeout, this, &MessageView::destroy);
    connect(item, &MessageItem::timeOut, this, &MessageView::destroy);
    this->viewList.push_back(item);
}

void MessageView::destroy(MessageItem *item)
{
    if (!this->viewList.empty())
    {
        // this->viewList.erase(item);
        auto it = std::remove(viewList.begin(), viewList.end(), item);
        viewList.erase(it, viewList.end());
        this->update();
    }
}

void MessageView::update()
{
    unsigned int count = 0;
    for (MessageItem *other : viewList)
    {
        other->setPosition(count++ * (messageItemHeight + spacing) + spacing);
    }
}

MessageItem::MessageItem(const QColor&color, const QString&text, const QString&iconSrc, const unsigned int msec, QWidget *parent)
    : QWidget{parent}, timer{}, text(text), time{msec}
{
    // this->text = QString(text);
    timer.setInterval(time);
    timer.setSingleShot(true);
    connect(&timer, &QTimer::timeout, this, &MessageItem::destroy);
    if (time > 0)
    {
        timer.start();
    }
    animation = new QPropertyAnimation(this, "pos");
    animation->setDuration(200);

    defalueWidth = (parent->width() > defalueWidth ? defalueWidth : parent->width() - 30);
    const unsigned int iconSize = defalueHeight / 3;
    this->setGeometry(parent->width() / 2 - defalueWidth / 2, -defalueHeight, defalueWidth, defalueHeight);
    this->setMinimumSize(defalueWidth - 50, defalueHeight);
    // this->setObjectName("messageItem");
    this->setStyleSheet(QString("MessageItem { "
                                "background-color: rgba(%1, %2, %3, 10%); "
                                "border-radius: 15px;"
                                "}"
                                "MessageItem QLabel {"
                                "color: rgba(%1, %2, %3, 100%);"
                                "font-size: 15px;"
                                "}")
                        .arg(color.red())
                        .arg(color.green())
                        .arg(color.blue()));
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    auto *icon = new QSvgWidget(iconSrc, this);
    icon->setGeometry(iconSize, defalueHeight / 2 - iconSize / 2, iconSize, iconSize);

    auto *mainText = new QLabel(text, this);
    mainText->setMinimumSize(defalueWidth / 2, defalueHeight);
    mainText->setGeometry(icon->x() + icon->width() + 5, 0, defalueWidth / 2, defalueHeight);
    mainText->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    auto *closeBtn = new QPushButton(this);
    closeBtn->setGeometry(this->width() - iconSize * 2, icon->y(), iconSize, iconSize);
    closeBtn->setStyleSheet(QString("    MessageItem QPushButton{\
        border-image: url(:/ui/icons/close-three.svg);\
        border-radius: %1px;\
        border: 2px solid black;\
    }").arg(iconSize / 2));
    connect(closeBtn, &QPushButton::clicked, this, &MessageItem::destroy);
    this->setAttribute(Qt::WA_StyledBackground);
}

void MessageItem::setPosition(const int y)
{
    const int x = this->x();
    this->animation->setStartValue(QPoint(x, this->y()));
    this->animation->setEndValue(QPoint(x, y));
    this->animation->start();
}

void MessageItem::destroy()
{
    emit this->timeOut(this);
    this->setPosition(-defalueHeight);
    connect(this->animation, &QPropertyAnimation::finished, this, &MessageItem::deleteLater);
}
